<?php
require_once("../PHPMySQLiDatabaseClass/MysqliDb.php");
require("../email.php");
require("../db_config.php");
$dbConfig = new dbConfig();
$db = new MysqliDb
(
    $dbConfig->host,
    $dbConfig->account,
    $dbConfig->pwd,
    'student');
$title   = $_POST['title'];
$content = $_POST['content'];
$user    = $_POST['user']; 
$check = $_POST['check_state'];#是否通过审核
$respone_body=array
(
    "code"=>false,
    "msg"=>"",
);
#审核码 1通过 -1不通过 0待审核
if($check == "true")
{
   // echo "过审核逻辑执行>";
    $data = Array("allow"=>1);
    #筛选条件 获取到任务主键id值就不会错
    #SELECT id FROM `task` WHERE title="$title"
    #UPDATE `task` SET `allow`=0 WHERE id=2
    $db->where("title",$title);
    $task_id = $db->getValue("task","id",1);
    //echo $task_id;
    
    $db->where("id",$task_id);
    if($db->update('task',$data))
    {
        $respone_body['code'] = true;
        $respone_body['msg']  = $db->count.'条几部被更新';
        //echo json_encode($respone_body);
        send($user,$title,true);
    }
    else
    {
        $respone_body['code'] = false;
        $respone_body['msg']  = '更新数据失败:'.$db->getLastError();
        echo json_encode($respone_body);
    }
}
else if($check == "false")
{
   // echo "不过审逻辑执行>";
    $data = Array("allow"=>-1);
    $db->where("title",$title);
    $db->where("user",$user);
    $db->where("allow",0);
    if($db->update ('task', $data))
    {
        //echo $db->count . ' records were updated';
        send($user,$title,false);
    } 
    else
    {
        $respone_body['code'] = false;
        $respone_body['msg']  = '更新数据失败:'.$db->getLastError();
        echo json_encode($respone_body);
    }
}
/**
 * @param string $p_user 用户账号
 * @param string $p_title 任务标题
 * @param bool $pass 审核状态
 */
function send($p_user,$p_title,$pass)
{
    $mail = new Email();
    $mail->set_type($mail->check);
    $mail->extra_title = $p_title;
    $mail->to_data['email'] = $p_user;
    $mail->to_data['name']  = "用户";
    if($pass)
    {
        $mail->extra_body = "已通过审核，如非本人操作，请忽略。";
    }
    else
    {
        $mail->extra_body = "未通过审核，请前往修改。如非本人操作，请忽略。";
    }
    $mail->send(null);
}
?>